//
//  AppDelegate.m
//  哎游
//
//  Created by 康守帅 on 14-12-31.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "CameraViewController.h"
#import "TravelsListViewController.h"
#import "MusicListViewController.h"
#import "MyViewController.h"
#import <ShareSDK/ShareSDK.h>
#import "sinaWiboViewController.h"
#import "WebColor.h"
#import "WeiboSDK.h"
#import "UserGuideViewController.h"
#import "LoginViewCnntroller.h"
#import "macro.h"
#import "LocalViewController.h"
#import "WeiboApi.h"




@interface AppDelegate ()<UITabBarControllerDelegate, UIImagePickerControllerDelegate, UINavigationBarDelegate, UITabBarDelegate>

@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //欢迎页面
    [NSThread sleepForTimeInterval:2.5f];
    
    NSString *path = [NSString stringWithFormat:@"%@",[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)firstObject]];
    NSLog(@"path = %@", path);
    
    // Override point for customization after application launch.
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    [_window makeKeyAndVisible];
    [_window setBackgroundColor:[UIColor whiteColor]];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    BOOL notFirstTime = [[user objectForKey:@"login"] boolValue];
    if (!notFirstTime) {
        [self makeGuideController];
    }else{
        [self makeController];
    }
    return YES;
}
    

- (void) makeGuideController
{
    UserGuideViewController *user = [[UserGuideViewController alloc] init];
    
    [_window addSubview:user.view];
    [user getGuideBlock:^{
        [self makeController];
    }];
}
    
- (void) makeController
{
    //首页视图 上面加个Nav
    MainViewController *main = [[MainViewController alloc] init];
    
    UINavigationController *navFirst = [[UINavigationController alloc] initWithRootViewController:main];
//    navFirst.navigationBar.translucent = NO;
    if (IOS_VERSION < 7.0) {
        navFirst.tabBarItem = [[UITabBarItem alloc] init];
        navFirst.tabBarItem.title = @"首页";
        [navFirst.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"shou2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"shou.png"]];
    }else
    {
    navFirst.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"shou2.png"] selectedImage:[UIImage imageNamed:@"shou.png"]];
    }
    
    
    if (IOS_VERSION > 7.0) {
        navFirst.navigationBar.barTintColor = [WebColor mediumSeaGreen];
    }
    // 游记视图
    TravelsListViewController *travels = [[TravelsListViewController alloc] init];
    UINavigationController *navSecond = [[UINavigationController alloc] initWithRootViewController:travels];
    navSecond.navigationBar.translucent = NO;
    
    if (IOS_VERSION < 7.0) {
        navSecond.tabBarItem = [[UITabBarItem alloc] init];
        navSecond.tabBarItem.title = @"游记";
        [navSecond.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"shu2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"shu.png"]];
    }else
    {
        navSecond.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"游记" image:[UIImage imageNamed:@"shu2.png"] selectedImage:[UIImage imageNamed:@"shu.png"]];
    }
    
    if (IOS_VERSION > 7.0) {
        navSecond.navigationBar.barTintColor = [WebColor mediumSeaGreen];
    }
    
    //照相机视图
    self.camera = [[CameraViewController alloc] init];
    
    if (IOS_VERSION < 7.0) {
        _camera.tabBarItem = [[UITabBarItem alloc] init];
        _camera.tabBarItem.title = @"相机";
        [_camera.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"xiangji2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"xiangji.png"]];
    }else
    {
    _camera.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"相机" image:[UIImage imageNamed:@"xiangji2.png"] selectedImage:[UIImage imageNamed:@"xiangji.png"]];
    }

    
    
    //音乐视图
    MusicListViewController *music = [[MusicListViewController alloc] init];
    UINavigationController *navForth = [[UINavigationController alloc] initWithRootViewController:music];
    navForth.navigationBar.translucent = NO;
 
    
    if (IOS_VERSION < 7.0) {
        navForth.tabBarItem = [[UITabBarItem alloc] init];
        navForth.tabBarItem.title = @"首页";
        [navForth.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"music2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"music.png"]];
    }else
    {
        navForth.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"音乐" image:[UIImage imageNamed:@"music2.png"] selectedImage:[UIImage imageNamed:@"music.png"]];
    }

    
    if (IOS_VERSION > 7.0) {
        navForth.navigationBar.barTintColor = [UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:100];
    }
    NSMutableArray *array = [ NSMutableArray array];
    
    
    //足迹视图
    sinaWiboViewController *sina = [[sinaWiboViewController alloc] init];
    UINavigationController *naLogin = [[UINavigationController alloc] initWithRootViewController:sina];
        naLogin.navigationBar.translucent = NO;
        naLogin.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"my.png"] tag:5];
        if (IOS_VERSION > 7.0) {
            naLogin.navigationBar.barTintColor = [WebColor seaGreen];
    }
   
    //全部nav 放在TabBar上面
    UITabBarController *tabBar = [[UITabBarController alloc] init];
    
    
    //判断当前版本, 执行下列代码
    NSString* strVersion = [[UIDevice currentDevice] systemVersion];
    float version = [strVersion floatValue];
    if (version > 7.0) {
        tabBar.tabBar.translucent = NO;
        tabBar.tabBar.tintColor = [UIColor blackColor];
        tabBar.tabBar.barTintColor = [UIColor whiteColor];
    }
    
    [array addObject:navFirst];
    [array addObject:navSecond];
    [array addObject:_camera];
    [array addObject:navForth];
    [array addObject:naLogin];

    tabBar.viewControllers = array;
    tabBar.delegate = self;
    tabBar.tabBar.tintColor = [WebColor mediumSeaGreen];
    [_window setRootViewController:tabBar];
    [tabBar release];
    [navFirst release];
    [navSecond release];
    [navForth release];
    [naLogin release];
    [main release];
    [travels release];
    [music release];
    
    [ShareSDK registerApp:@"52d60b5f1854"];//字符串api20为您的ShareSDK的AppKey
    
    //添加新浪微博应用 注册网址 http://open.weibo.com
    [ShareSDK connectSinaWeiboWithAppKey:@"1963869471"
                               appSecret:@"8820acf07cda81543c2751e8464a5fbc"
                             redirectUri:@"http://www.lanou3g.com" ];
    [ShareSDK  connectSinaWeiboWithAppKey:@"568898243"
                                appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
                              redirectUri:@"http://www.sharesdk.cn"
                              weiboSDKCls:[WeiboSDK class]];
}


- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait;
}

//对url进行请求
//- (BOOL) application:(UIApplication *) application handleOpenURL:(NSURL *)url
//{
//    return [ShareSDK handleOpenURL:url wxDelegate:nil];
//}
//
//- (BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
//{
//    return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:nil];
//}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    NSLog(@"内存警告");
}

//
//- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
//{
//    return YES;
//}
//
//- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
//{
//    if (viewController == [tabBarController.viewControllers objectAtIndex:2]) {
//        if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
//            UIImagePickerController *imagePick = [[UIImagePickerController alloc] init];
////            imagePick.delegate = self;
//            imagePick.sourceType = UIImagePickerControllerSourceTypeCamera;
//            [imagePick takePicture];
////            imagePick.allowsEditing = YES;
//            [_camera presentViewController:imagePick animated:YES completion:nil];
//            [imagePick release];
//        }else{
//            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无前置相机设备, 设备不可用" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alert show];
//            [alert release];
//            [_camera dismissViewControllerAnimated:YES completion:nil];
//        }
//    }
//}
//
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
//{
//    NSLog(@"选取图片调用");
//    //    self.imageView.image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
////    [picker dismissViewControllerAnimated:YES completion:nil];
//    //    dismissModalViewControllerAnimated:YES];
////    [picker isMovingToParentViewController];
////    [picker isMovingFromParentViewController];
//        [_camera presentViewController:picker animated:YES completion:nil];
//    
//    // NSValue * cropRect = [info objectForKey:UIImagePickerControllerCropRect];
//    // CGRect rect = [cropRect CGRectValue];
//    
//}
//
//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
//{
//    NSLog(@"取消选取");
//    [picker dismissViewControllerAnimated:YES completion:nil];
//}

//void UIImageWriteToSavedPhotosAlbum (
//      UIImage  *image,
//      id       completionTarget,
//      SEL      completionSelector,
//      void     *contextInfo
//);



//- (void)saveImageToPhotos:(UIImage*)savedImage 
//{
//    UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);  
//}
//// 指定回调方法
//- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
//{   
//    NSString *msg = nil ;   
//    if(error != NULL){
//        msg = @"保存图片失败" ;
//    }else{
//        msg = @"保存图片成功" ;
//    }
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
//    [alert show];
//}


//// 调用示例
//UIImage *savedImage = [UIImage imageNamed:"savedImage.png"];
//
//[self saveImageToPhotos:savedImage];

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // 进入后台
    [application beginReceivingRemoteControlEvents];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
